package it.cast.wechat.cotroller;

import it.cast.wechat.dto.OrderVo;
import it.cast.wechat.dto.Result;
import it.cast.wechat.pojo.Orders;
import it.cast.wechat.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
public class OrderController {

    @Autowired
    private OrdersService orderService;

    @RequestMapping("orders/orderList")
    public String orderList(Map<String,Object> map){
        List<OrderVo> ordersList=orderService.orderList();
        map.put("ordersList",ordersList);

        return "admin/order/list";
    }
    @RequestMapping("wx/orders/queryOrder")
    @ResponseBody
    public List<OrderVo> orderList(String orderId,String userId){
        List<OrderVo> ordersList = orderService.queryOrderByUserId(userId,orderId);
        return ordersList;
    }

    @PostMapping("orders/order")
    @ResponseBody
    public Result<String> order(@RequestBody OrderVo orderVo){
        Result<String> result = new Result<>();
        try {
            String orderId = orderService.saveOrder(orderVo);
            result.setData(orderId);
        }catch (Exception e){
            result.setCode(100);
            System.out.println(e.getMessage());
            result.setErrorMsg(e.getMessage());
        }

        return result;
    }

    @GetMapping("orders/updateOrder")
    public String updateOrder(Map<String,Object> map, Orders orders){
        int state = orderService.updateOrder(orders);
        List<OrderVo> ordersList=orderService.orderList();
        map.put("ordersList",ordersList);
        return "admin/order/list";
    }

    //更新状态
    @GetMapping("/wx/orders/updateOrderSate")
    @ResponseBody
    public Result<OrderVo> updateOrder(Orders orders){
        Result<OrderVo> result = new Result<>();
        try {
            orderService.updateOrder(orders);
            OrderVo orderVo = orderService.queryOrderByOrderId(orders.getOrderId());
            result.setData(orderVo);
            result.setCode(200);
        }catch (Exception e){
            result.setCode(100);
            System.out.println(e.getMessage());
            result.setErrorMsg(e.getMessage());
        }
        return result;
    }

}
